package com.zcxx.controller;

import com.zcxx.common.Result;
import com.zcxx.model.Employee;
import com.zcxx.model.User;
import com.zcxx.service.EmployeeService;
import com.zcxx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;

/**
 * @Author WangShuai
 * @Date 2025/2/28 19:50
 * @Version 1.0
 * @Description 负责页面渲染$
 */
@Controller
@RequestMapping("/employee")
@Api(tags = "员工管理视图", description = "管理系统用户视图的增删改查操作")
public class EmployeeViewController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private UserService userService;

    //显示添加员工页面
    @GetMapping("/add")
    @ApiOperation(value = "显示添加员工页面", notes = "显示用于添加员工的表单页面")
    @ApiResponses({
            @ApiResponse(code = 200, message = "显示成功")
    })
    public String showAddEmployeeForm(Model model) {
        return "add-employee"; // Name of the Thymeleaf template
    }

    //处理添加员工页面发送来的表格，并重定向显示信息
    @PostMapping("/save")
    @ApiOperation(value = "保存员工信息", notes = "处理添加员工表单提交的数据并保存")
    @ApiResponses({
            @ApiResponse(code = 302, message = "重定向到员工列表页面"),
            @ApiResponse(code = 500, message = "保存失败")
    })
    public String saveEmployee(
            @ApiParam(name = "employee", value = "员工信息", required = true) @ModelAttribute Employee employee,
            @ApiParam(name = "file", value = "员工照片", required = true) @RequestPart MultipartFile file) throws IOException {
        employeeService.saveEmployee(employee, file);
        return "redirect:/employee/list"; // Redirect to the employee list
    }

    //根据ID删除员工
    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除员工", notes = "根据员工ID删除员工")
    @ApiResponses({
            @ApiResponse(code = 302, message = "重定向到员工列表页面"),
            @ApiResponse(code = 500, message = "删除失败")
    })
    public String deleteEmployee(@ApiParam(name = "id", value = "员工ID", required = true) @PathVariable Integer id) throws IOException {
        employeeService.deleteEmployeeById(id);
        return "redirect:/employee/list";
    }

    //显示编辑员工页面
    @GetMapping("/edit/{id}")
    @ApiOperation(value = "显示编辑员工页面", notes = "显示用于编辑员工的表单页面")
    @ApiResponses({
            @ApiResponse(code = 200, message = "显示成功"),
            @ApiResponse(code = 404, message = "员工不存在")
    })
    public String editEmployeeForm(@ApiParam(name = "id", value = "员工ID", required = true) @PathVariable Integer id, Model model) {
        Employee result = employeeService.getById(id);
        if (result != null) {
            model.addAttribute("employee", result);
            return "employee-edit";
        } else {
            return "error-page";
        }
    }

    //处理编辑员工页面发送来的表格，并重定向显示信息
    @PostMapping("/edit/{id}")
    @ApiOperation(value = "更新员工信息", notes = "处理编辑员工表单提交的数据并更新")
    @ApiResponses({
            @ApiResponse(code = 302, message = "重定向到员工列表页面"),
            @ApiResponse(code = 404, message = "员工不存在"),
            @ApiResponse(code = 500, message = "更新失败")
    })
    public String editEmployee(
            @ApiParam(name = "employee", value = "员工信息", required = true) @ModelAttribute Employee employee,
            @ApiParam(name = "photo", value = "员工照片", required = false) @RequestPart(required = false) MultipartFile photo) throws IOException {
        String result = employeeService.updateEmployee(employee, photo);
        if (result != null) {
            return "redirect:/employee/list";
        } else {
            return "error-page";
        }
    }

    //显示员工列表
    @GetMapping("/list")
    @ApiOperation(value = "显示员工列表", notes = "显示所有员工的信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "显示成功")
    })
    public String showEmployeeList(Model model) {
        model.addAttribute("employees", employeeService.list());
        return "employee-list"; // Name of the Thymeleaf template
    }

    /**
     * 获取员工详细信息
     *
     * @param id    员工ID，用于标识特定的员工
     * @param model 用于将员工对象传递给视图
     * @return 返回员工详细信息页面的视图名称
     */
    @GetMapping("/detail/{id}")
    @ApiOperation(value = "获取员工详细信息", notes = "根据员工ID获取员工详细信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "显示成功"),
            @ApiResponse(code = 404, message = "员工不存在")
    })
    public String getEmployeeDetail(@ApiParam(name = "id", value = "员工ID", required = true) @PathVariable Integer id, Model model) {
        Employee employee = employeeService.getById(id);
        model.addAttribute("employee", employee);
        return "employee-detail";
    }
}
